package com.boot.web.exception;

import com.boot.web.http.Status;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.core.NestedRuntimeException;
import org.springframework.http.HttpStatus;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 全局异常
 * @date 2019/5/20 19:03
 **/
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class GlobalException extends NestedRuntimeException {
    /**
     * @description 业务状态
     * @date 2019/7/26 14:31
     **/
    private Status status;

    /**
     * @description 针对此业务状态要返回的 HTTP 状态
     * @date 2019/7/26 14:31
     **/
    private HttpStatus httpStatus;

    public GlobalException(Status status, HttpStatus httpStatus) {
        this(status, null, httpStatus);
    }

    public GlobalException(Status status, String msg, HttpStatus httpStatus) {
        super(msg);
        this.status = status;
        this.httpStatus = httpStatus;
    }

    public GlobalException(Status status, HttpStatus httpStatus, Throwable cause) {
        this(status, null, httpStatus, cause);
    }

    public GlobalException(Status status, String msg, HttpStatus httpStatus, Throwable cause) {
        super(msg, cause);
        this.status = status;
        this.httpStatus = httpStatus;
    }

    public String getRawMessage() {
        return super.getMessage();
    }

    @Override
    public String getMessage() {
        if (super.getMessage() == null) {
            return status.toString() + " , " + httpStatus.value() + " : " + httpStatus.getReasonPhrase();
        }
        return status.toString() + " , " + super.getMessage() + " , " + httpStatus.value() + " : " + httpStatus.getReasonPhrase();
    }
}
